const duration = 2000;
/**
 * @param {String} title 
 * @param {String} icon 
 * @returns Promise 
 */
function showToast(title, icon, image) {
    return new Promise((resolve, reject) => {
        wx.showToast({
            title: title,
            icon: icon,
            image: image,
            duration: duration,
            success: () => {
                setTimeout(() => {
                    resolve(true)
                }, duration)
            },
            fail: () => {
                reject(false)
            }

        })
    })
}

function showLoading(title) {
    wx.showLoading({
        title:title || '',
        mask: true
    })
}

function navigatTo(path) {


    return new Promise((resolve, reject) => {
        wx.navigateTo({
            url: path,
            success: function(res) {
                resolve(true)
            },
            fail: function(err) {

                reject(false)
            },
            complete: function() {}
        })
    })
}

function redirectTo(path) {

    return new Promise((resolve, reject) => {

        wx.redirectTo({
            url: path,
            success: function() {
                resolve(true)
            },
            fail: function() {
                reject(false)
            }
        })

    })

}

function relaunch(path){

    return new Promise((resolve,reject)=>{

        wx.reLaunch({
            url:path,
            success:function(){
                resolve(true)
            },
            fail:function(){
                reject(false)
            }
        })

    })

}

function getUserInfo() {

    return new Promise((resolve, reject) => {

        wx.login({
            success: function(res) {



                resolve(res);
                // wx.getUserInfo({
                //     withCredentials: true,
                //     success: (res)=> {
                //         

            //         resolve(res)
            //     },
            //     fail:()=>{
            //         reject('获取用户信息失败')
            //     }
            // })
            },
            fail: function() {
                reject('登录失败')
            },
            complete: function() {
                // complete
            }
        })

    })

}

/**
 * 获取地中中点
 * @param {Object} map mapContext 对象
 * @returns 
 */
function getMapCenter(map) {

    return new Promise((resolve, reject) => {

        map.getCenterLocation({
            success: function(res) {

                resolve(res)
            },
            fail: function() {

                reject('无法获取地图中点坐标')
            }
        })

    })

}

function chooseLocation() {

    return new Promise((resolve, reject) => {

        wx.chooseLocation({
            success: (res) => {
                wx.hideLoading();
                if(res.address){
                    resolve(res)
                }else{
                    reject('获取地址失败,请重试')
                }
                // this.setUserInfoData(type, res.address)
                // this.setUserInfoData(type + 'Point', {
                //     lng: res.longitude,
                //     lat: res.latitude
                // })

                // app.orderData[type] = res.address;
                // app.orderData[type + 'Point'] = {
                //     lng: res.longitude,
                //     lat: res.latitude
                // }

            },

            fail(){
                reject('获取地址失败,请重试')
            },
            complete: function(res) {
                wx.hideLoading();

                if (res.errMsg != 'chooseLocation:ok') {
                    // wx.showToast({
                    //     title: '获取地址失败,请重试',
                    //     image: '/resources/images/error3.png',
                    //     duration: 3000
                    // })
                    reject('获取地址失败,请重试')

                }

            }
        })

    })

}

module.exports = {
    showToast: showToast,
    showLoading: showLoading,
    navigateTo: navigatTo,
    getUserInfo: getUserInfo,
    getMapCenter: getMapCenter,
    redirectTo: redirectTo,
    chooseLocation:chooseLocation,
    relaunch:relaunch
}